package com.lion.common.modules.file.service.impl;

import com.chris.dev.base.libs.v1.http.params.ResourceParam;
import com.chris.dev.base.libs.v1.http.results.ResourceResult;
import com.lion.common.modules.file.common.config.ConditionalOnFileStrategy;
import com.lion.common.modules.file.common.enums.MediaTypeEnum;
import com.lion.common.modules.file.common.utils.LocalFileUtil;
import com.lion.common.modules.file.domain.dto.SaveFileInfo;
import com.lion.common.modules.file.service.assistants.FileAssistant;
import com.lion.common.modules.file.service.def.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Chris Chan
 * Create On 2025/8/28 下午3:52
 * Use for: 文件系统 服务实现
 * Explain:
 */
@Service
@ConditionalOnFileStrategy("local")
public class LocalFileServiceImpl implements FileService {

    @Value("${file.local.base-dir:/tmp/uploads}")
    private String baseDir;

    @Value("${file.local.base-url:http://localhost:8080/uploads}")


    private String baseUrl;
    @Autowired
    private FileAssistant fileAssistant;

    @Override
    public ResourceResult uploadWithName(String fileName, MultipartFile file) {
        //1. 获取并转换媒体类型
        MediaTypeEnum mediaType = FileAssistant.getMediaType(file);
        //2. 构建存储文件信息:存储文件名 存储文件位置 URI
        SaveFileInfo saveFileInfo = fileAssistant.buildSaveFileInfo(1, mediaType, fileName);
        //3. 保存文件 失败抛异常
        LocalFileUtil.copy(file, saveFileInfo.getPath());
        //4. 构建结果返回
        return ResourceResult.of(saveFileInfo.getUrl(), saveFileInfo.getUri());
    }

    @Override
    public ResourceResult uploadFile(int mode, MultipartFile file) {
        //1. 获取文件名
        String fileName = file.getOriginalFilename();
        //2. 获取并转换媒体类型
        MediaTypeEnum mediaType = FileAssistant.getMediaType(file);
        //3. 构建存储文件信息:存储文件名 存储文件位置 URI
        SaveFileInfo saveFileInfo = fileAssistant.buildSaveFileInfo(mode, mediaType, fileName);
        //4. 保存文件 失败抛异常
        LocalFileUtil.copy(file, saveFileInfo.getPath());
        //5. 构建结果返回
        return ResourceResult.of(saveFileInfo.getUrl(), saveFileInfo.getUri());
    }

    @Override
    public ResourceResult uploadBase64(ResourceParam param) {
        //1. 解析参数
        String fileName = param.getFileName();
        int type = param.getType();
        String contentBase64 = param.getContentBase64();
        //2. 内容解码
        byte[] contentBytes = fileAssistant.parseBase64(contentBase64);
        //3. 类型检查 有错误抛异常
        fileAssistant.checkType(type, contentBase64);
        //4. 构建存储文件信息:存储文件名 存储文件位置 URI(需要加一个参数指明媒体类型，或者从数据中解析)
        SaveFileInfo saveFileInfo = fileAssistant.buildSaveFileInfo(type, MediaTypeEnum.FILE, fileName);
        //5. 将字节数组保存到文件 出错抛异常
        LocalFileUtil.save(contentBytes, saveFileInfo.getPath());
        //6. 构建结果返回
        return ResourceResult.of(saveFileInfo.getUrl(), saveFileInfo.getUri());
    }
}
